package com.client.core.service;

import com.alibaba.fastjson.JSONArray;
import com.client.core.ChatClient;
import com.client.core.file.FileHandler;
import com.common.MsgUtil;

import java.io.File;
import java.util.Scanner;

public class ChatService {

    private Scanner scanner = new Scanner(System.in);
    public static String me;
    public static String he;

    public static JSONArray groups;

    public ChatService() {

        while (true) {
            System.out.println("请输入一个账号：");
            String next = scanner.next();
            if (next == null || next.trim().length() == 0) {
                continue;
            } else {
                ChatService.me = next;
                break;
            }
        }

        ChatClient chatClient = new ChatClient("127.0.0.1", 8888);
        try {
            chatClient.start();
            ChatClient.channel.writeAndFlush(MsgUtil.CONNECT(me, "连接数据"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 获取用户组列表
     */
    public void getGroup() {
        ChatClient.channel.writeAndFlush(MsgUtil.GET_GROUP(me));
        System.out.println("请求已经发出");
    }

    /**
     * 选择用户对象
     */
    public void selectGroup() {
        while (true) {
            try {
                if (groups == null) {
                    System.out.println("服务器用户组为空,请重新获取");
                    return;
                }
                int i = 0;
                for (Object group : groups) {
                    if (me.equals(group.toString())) {
                        System.out.println(i + "-->" + group.toString() + "(我自己)");
                    } else {
                        System.out.println(i + "-->" + group.toString());
                    }
                    i++;
                }

                System.out.println("请输入标号，选择对象：");
                int anInt = scanner.nextInt();
                Object o = groups.get(anInt);
                ChatService.he = o.toString();
                break;
            } catch (Exception e) {
                System.out.println("选择异常，请重新选择");
                continue;
            }
        }
        System.out.println("用户对象选择完毕，你选择的是（" + ChatService.he + ")");
    }


    public void chatMsg() {
        if (he == null) {
            System.out.println("请选择用户对象在进行聊天");
            return;
        }
        System.out.println("已经进入聊天模式，按 'q' 回车退出");
        while (true) {
            System.out.println("请输入内容");
            String next = scanner.next();
            if ("q".equals(next)) {
                break;
            }
            ChatClient.channel.writeAndFlush(MsgUtil.SEND_MSG(me, he, next));
        }
    }

    public void chatFile() {
        if (he == null) {
            System.out.println("请选择用户对象在进行发送文件");
            return;
        }
        while (true) {
            System.out.println("请输入文件拆分大小（kb）保证输入的值比你的文件小，同时不要超过512Kb");
            FileHandler.FILESIZE = scanner.nextInt();
            System.out.println("请输入一个文件路径:");
            String next = scanner.next();
            System.out.println("文件拆分需要一点时间请等待...");
            if (next != null && next.trim().length() > 0) {
                String wokerpath = System.getProperty("user.dir");
                //创建一个工作文件夹
                String s = wokerpath + File.separator + "workerFile" + File.separator + "upload";
                File file = new File(s);
                if (!file.exists()) {
                    file.mkdirs();
                }
                //将要操作的文件考培到这里
                File file1 = new File(next);
                String name = file1.getName();
                File tmp_file = new File(file.getAbsolutePath() + File.separator + name);
                //清空文件
                FileHandler.delFile(tmp_file);
                tmp_file.mkdirs();
                File file2 = new File(tmp_file.getAbsolutePath() + File.separator + name);
                if (file1.isFile()) {
                    try {
                        FileHandler.copyFile(file1, file2);
                    } catch (Exception e) {
                        System.out.println("文件拷贝失败，重新试试");
                    }
                } else {
                    System.out.println("这个不是一个文件请重新选择");
                    continue;
                }
                //对文件进行拆分洛
                try {
                    FileHandler.splitFile(file2.getAbsolutePath());
                } catch (Exception e) {
                    System.out.println("拆分文件异常：" + e.getMessage());
                    continue;
                }
                //发送文件拆分文件给对方
                String string = FileHandler.getString(new File(file2 + "拆分说明.tmp"));
                ChatClient.channel.writeAndFlush(MsgUtil.START_FILE(me, he, string.getBytes()));
                //文件拆分了，发送一个拆分说明过去，对方自动读取本机文件，所以可以退出这个循环了
                break;
            } else {
                continue;
            }
        }

    }


    public void start() {
        while (true) {
            System.out.println("1.获取最新在线用户列表，2.选择用户对象，3.和对象聊天，4.发文件给对象");
            System.out.println("请输入编号进行选择：");
            switch (scanner.nextInt()) {
                case 1:
                    getGroup();
                    break;
                case 2:
                    selectGroup();
                    break;
                case 3:
                    chatMsg();
                case 4:
                    chatFile();
            }
        }

    }
}
